package cn.sumpu.app.guoguojie.util;

import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.text.StringCharacterIterator;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collection;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Random;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletRequest;

import cn.sumpu.app.guoguojie.exception.AppBusinessException;

public class Common {
    public static int           pageIndex                     = 1;

    public static final int     PAGE_SIZE                     = 20;

    public static final int     PAGE_SIZE_FOLLOW              = 20;

    public static final int     PAGE_SIZE_DOING_COMMENT       = 10;

    public static final int     PAGE_SIZE_DISCUSS_COMMENT     = 20;

    public static final int     PAGE_SIZE_FAMILY_COMMENT      = 40;

    public static final int     PAGE_SIZE_ADMIN               = 10;

    public static final int     PAGE_SIZE_DOORPLATE           = 5;

    public static final int     PAGE_SIZE_ALBUM               = 12;

    public static final int     PAGE_SIZE_TOPIC_RECOMEND      = 7;

    public static final int     PAGE_SIZE_TOPIC_COMMON        = 20;

    public static final int     PAGE_SIZE_TOPIC_DETAIL        = 20;

    public static final int     PAGE_SIZE_CROWD               = 12;

    public static final int     PAGE_SIZE_FRIEND              = 12;

    public static final int     PAGE_SIZE_TROUBLE_FRIEND      = 5;

    public static final int     PAGE_SIZE_MAYBE_FRIEND_MAIN   = 5;

    public static final int     PAGE_SIZE_VISITOR             = 32;

    public static final int     PAGE_SIZE_MESSAGE             = 10;

    public static final int     PAGE_SIZE_DOING               = 20;

    public static final int     PAGE_SIZE_PICTORIAL           = 10;

    public static final int     PAGE_SIZE_PICTORIAL_6         = 6;

    public static final int     PAGE_SIZE_PICTORIAL_2         = 2;

    public static final int     PAGE_SIZE_SEARCH              = 20;

    public static final int     PAGE_SIZE_DOING_LOVE          = 10;

    public static final int     PAGE_SIZE_ACTIVITY_CROWD      = 3;

    public static final int     PAGE_SIZE_ACTIVITY_COMMUNITY  = 2;

    public static final int     PAGE_SIZE_ACTIVITY_COMPANY    = 2;

    public static final int     PAGE_SIZE_ACTIVITY_REGISTERED = 4;

    public static final int     PAGE_SIZE_ACTIVITY_WEEK       = 4;

    public static final int     PAGE_SIZE_ACTIVITY_LIST       = 10;

    public static final int     PAGE_SIZE_TIPS_COMMUNITY      = 6;

    public static final int     PAGE_SIZE_MY_FAMILY_FRIEND    = 6;

    public static final int     PAGE_SIZE_MY_FAMILY_VISITOR   = 6;

    public static final int     PAGE_SIZE_ACTIVITY_FAMILY     = 10;

    public static final int     PAGE_SIZE_CROWD_MEMBER        = 25;

    public static final int     PAGE_SIZE_POSTDOING_MYFAMILY  = 20;

    public static final int     PAGE_SIZE_PIC_DOING           = 10;

    public static final int     PAGE_SIZE_TCOVER_FEED         = 3;

    public static final int     PAGE_SIZE_CROWD_MEMBER_MAIN   = 6;

    public static final int     PAGE_SIZE_COMPANY_PRODUCT     = 7;

    public static final int     PAGE_SIZE_CROWD_MESSAGE       = 20;

    public static final int     PAGE_SIZE_COMMUNITY_BULLETIN  = 20;

    public static final int     PAGE_SIZE_COMMUNITY_MAIN      = 3;

    public static final int     PAGE_SIZE_MAYBECROWD_MAIN     = 4;
    // 捣乱动态显示的条数
    public static final int     PAGE_SIZE_TOUBLE_DYNAMIC      = 50;

    public static final int     PAGE_SIZE_BLOG_COMMENT        = 20;

    public static final int     PAGE_SIZE_BLOG_LIST           = 10;

    public static final int     PAGE_SIZE_PRODUCT_ORDER       = 8;

    public static final int     PAGE_SIZE_COMPANY_LISTENER    = 8;

    public static final int     PAGE_SIZE_MEMBER_ACTIVE_DOING = 30;

    public static final String  AVATAR_MEMBER_DEFAULT         = "/images/v4/common/default_avate.png";

    public static final String  AVATAR_FAMILY_DEFALUT         = "images/avatar/family.jpg";

    public static final String  AVATAR_MAN_DEFALUT            = "images/avatar/man.jpg";

    public static final String  AVATAR_WOMAN_DEFALUT          = "images/avatar/woman.jpg";

    public static final String  AVATAR_BABY_DEFALUT           = "images/avatar/baby.jpg";

    public static final String  ALBUM_DEFALUT                 = "images/avatar/album.jpg";

    public static final String  LOGO_CROWD_DEFALUT            = "images/avatar/family.jpg";

    public static final long    PHOTO_SIZE_ALLOW              = 2048000;

    public static final String  PHOTO_POST_SERVER             = "http://192.168.0.105:8080/";

    public static final String  PHOTO_POST_SERVER_SERVICE     = PHOTO_POST_SERVER + "receiveImage.htm";

    public static final String  DOORPLATE_GUOGUOXIAOJIA       = "888888";

    public static final String  SINAWEIBO_NAME_GUOGUOJIE      = "果果街妈妈购物分享";

    public static final String  TENCENTWEIBO_NAME_GUOGUOJIE   = "果果街";

    private static final String CHARSET                       = "UTF-8";
    private static final String randChars                     = "0123456789abcdefghigklmnopqrstuvtxyzABCDEFGHIGKLMNOPQRSTUVWXYZ";
    private static Random       random                        = new Random();

    public static int           THUMB_WIDTH                   = 153;
    public static int           THUMB_HEIGHT                  = 112;

    public static int           THUMB_WIDTH_100               = 100;
    public static int           THUMB_HEIGHT_100              = 100;

    public static int           THUMB_WIDTH_200               = 200;
    public static int           THUMB_HEIGHT_200              = 200;
    public static int           THUMB_WIDTH_320               = 320;
    public static int           THUMB_WIDTH_150               = 150;
    public static int           THUMB_HEIGHT_160              = 160;
    public static int           ORG_WIDTH                     = 1024;

    public static int           ORG_WIDTH_DOING               = 460;

    public static int           ORG_WIDTH_ACTIVITY            = 149;

    public static int           ORG_WIDTH_CROWD               = 149;

    public static int           ORG_WIDTH_COMPANY             = 120;

    public static int           ORG_WIDTH_COMMUNITY           = 120;

    public static int           ORG_WIDTH_PRODUCT             = 356;

    public static String        COMMUNITY_BLOG_TIPS           = "07";

    public static int           MAX_TROUBLE                   = 4;

    public static int           CAN_TROUBLE                   = 9;

    public static final String  TROUBLE_TYPE                  = "1";
    public static final String  CLEAN_TYPE                    = "2";
    public static final String  DEFENSE_TYPE                  = "3";

    public static final int     TROUBLE_POWER_REWARD          = 10;
    // 系统清洁道具的ID
    public static final String  TROUBLE_PROPERTY_SYSTEM_ID    = "1";

    public static final String  WEIBO_SINA_APP_KEY            = "3584234599";

    public static final String  WEIBO_SINA_APP_SECRET         = "35e53d1427503ad2e51119b7d012d0aa";

    public static final String  WEIBO_TENCENT_APP_KEY         = "801002608";

    public static final String  WEIBO_TENCENT_APP_SECRET      = "1f9f0c5f56c94fa5c34fca191c4424db";

    public static final String  WEIBO_SINA_GUOGUOJIE_UID      = "2129665095";

    public static final String  WEIBO_TENCENT_GUOGUOJIE_UID   = "guoguojie2011";

    public static final String  TENCENT_CLIENT_ID             = "100224191";
    public static final String  TENCENT_CLIENT_KEY            = "4bfe8ba2444ca078df311e74c547ba9c";
    public static final String  TENCENT_CLIENT_REDIRECT_URL   = "http://www.guoguojie.com/qqloginback.htm";
    // 年月日时分秒对应的毫秒数(月是按30天)
    public static final long    second                        = 1000;
    public static final long    minute                        = second * 60;
    public static final long    hour                          = minute * 60;
    public static final long    day                           = hour * 24;
    public static final long    month                         = day * 30;
    public static final long    year                          = month * 12;

    public static final String  NO_TOPIC_ID                   = "empty";
    public static final String  GUOGUOJIE_ACCOUNT             = "000000";
    public static final String  GUOGUOJIE_ACCOUNT_ID          = "d9c98063-88b9-4807-8ab4-e69c91c2d811";
    public static final String  GUOUOJIE_COST_ACCOUNT         = "aaaaaa";

    @SuppressWarnings("unchecked")
    public static boolean empty(Object obj) {
        if (obj == null) {
            return true;
        } else if (obj instanceof String && (obj.equals("") || obj.equals("0"))) {
            return true;
        } else if (obj instanceof Number && ((Number) obj).doubleValue() == 0) {
            return true;
        } else if (obj instanceof Boolean && !((Boolean) obj)) {
            return true;
        } else if (obj instanceof Collection && ((Collection) obj).isEmpty()) {
            return true;
        } else if (obj instanceof Map && ((Map) obj).isEmpty()) {
            return true;
        } else if (obj instanceof Object[] && ((Object[]) obj).length == 0) {
            return true;
        }
        return false;
    }

    /**
     * author Bruce Zhang
     * 
     * @param str
     * @return 如果字符串不为null也不为空字符串返回true
     */
    public static boolean notEmptyStr(String str) {
        if (str != null && !"".equals(str)) {
            return true;
        }
        return false;
    }

    public static boolean notNull(String str) {
        if (str != null) {
            return true;
        }
        return false;
    }

    public static boolean emptyStr(String str) {
        if (str == null || "".equals(str)) {
            return true;
        }
        return false;
    }

    public static String trim(String text) {
        if (text == null) {
            return "";
        }
        return text.trim();
    }

    public static String urlEncode(String s) {
        return urlEncode(s, CHARSET);
    }

    public static String urlEncode(String s, String enc) {
        if (!empty(s)) {
            try {
                return URLEncoder.encode(s, enc);
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            }
        }
        return s;
    }

    public static String urlDecode(String s) {
        return urlDecode(s, CHARSET);
    }

    public static String urlDecode(String s, String enc) {
        if (!empty(s)) {
            try {
                return URLDecoder.decode(s, enc);
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            }
        }
        return s;
    }

    public static String md5(String arg0) {
        return Md5Util.encode(arg0);
    }

    public static String getRandStr(int length, boolean isOnlyNum) {
        int size = isOnlyNum ? 10 : 62;
        StringBuffer hash = new StringBuffer(length);
        for (int i = 0; i < length; i++) {
            hash.append(randChars.charAt(random.nextInt(size)));
        }
        return hash.toString();
    }

    public static Date parseDate(String dateStr, String format) throws ParseException {
        if (empty(dateStr))
            return null;
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(format);
        return simpleDateFormat.parse(dateStr);
    }

    public static int rand(int max) {
        return rand(0, max);
    }

    public static int rand(int min, int max) {
        if (min < max) {
            return random.nextInt(max - min + 1) + min;
        } else {
            return min;
        }
    }

    public static String getFormatDate(Date date) {
        return getFormatDate(date, "yyyyMMdd");
    }

    public static String getFormatDate(Date date, String format) {
        DateFormat f = new SimpleDateFormat(format);
        return f.format(date);
    }

    public static String getCountFormat(int count) {
        String countStr = "";
        if (count > 99) {
            countStr = "99+";
        } else if (count > 0) {
            countStr = "+" + count;
        }
        return countStr;
    }

    public static String getWeekDay(Date date) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        int weekday = cal.get(Calendar.DAY_OF_WEEK);
        String weekDayFormat = "";
        switch (weekday) {
            case 1:
                weekDayFormat = "星期天";
                break;
            case 2:
                weekDayFormat = "星期一";
                break;
            case 3:
                weekDayFormat = "星期二";
                break;
            case 4:
                weekDayFormat = "星期三";
                break;
            case 5:
                weekDayFormat = "星期四";
                break;
            case 6:
                weekDayFormat = "星期五";
                break;
            case 7:
                weekDayFormat = "星期六";
                break;
        }

        return weekDayFormat;
    }

    public static String htmlSpecialChars(String string) {
        return htmlSpecialChars(string, 1);
    }

    public static String htmlSpecialChars(String text, int quotestyle) {
        if (text == null || text.equals("")) {
            return "";
        }
        StringBuffer sb = new StringBuffer(text.length() * 2);
        StringCharacterIterator iterator = new StringCharacterIterator(text);
        char character = iterator.current();
        while (character != StringCharacterIterator.DONE) {
            switch (character) {
                case '&':
                    sb.append("&amp;");
                    break;
                case '<':
                    sb.append("&lt;");
                    break;
                case '>':
                    sb.append("&gt;");
                    break;
                case '"':
                    if (quotestyle == 1 || quotestyle == 2) {
                        sb.append("&quot;");
                    } else {
                        sb.append(character);
                    }
                    break;
                case '\'':
                    if (quotestyle == 2) {
                        sb.append("&#039;");
                    } else {
                        sb.append(character);
                    }
                    break;
                default:
                    sb.append(character);
                    break;
            }
            character = iterator.next();
        }
        return sb.toString();
    }

    public static String nl2br(String text) {
        if (text == null || text.length() == 0) {
            return text;
        }
        StringBuffer sb = new StringBuffer(text.length() * 2);
        StringCharacterIterator iterator = new StringCharacterIterator(text);
        char character = iterator.current();
        while (character != StringCharacterIterator.DONE) {
            switch (character) {
                case '\r':
                    sb.append("<br/>");
                    sb.append(character);
                    character = iterator.next();
                    if (character == '\n') {
                        character = iterator.next();
                    }
                    break;
                case '\n':
                    sb.append("<br/>");
                    sb.append(character);
                    character = iterator.next();
                    if (character == '\r') {
                        sb.append(character);
                        character = iterator.next();
                    }
                    break;
                default:
                    sb.append(character);
                    character = iterator.next();
                    break;
            }
        }
        return sb.toString();
    }

    public static String cutStr(String str, int length, String fix) {
        if (empty(str))
            return str;
        if (length > str.length())
            return str;
        return str.substring(0, length) + fix;
    }

    public static String addDate(String day, int x) {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date = null;
        try {
            date = format.parse(day);
        } catch (ParseException ex) {
            ex.printStackTrace();
        }
        if (date == null)
            return "";
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.add(Calendar.DAY_OF_MONTH, x);
        date = cal.getTime();
        cal = null;
        return format.format(date);
    }

    public static Date addDate(Date date, int x) {
        if (date == null)
            return null;
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.add(Calendar.DAY_OF_MONTH, x);
        date = cal.getTime();
        cal = null;
        return date;
    }

    public static Date addMinutes(Date date, int minutes) {
        if (date == null)
            return null;
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.add(Calendar.MINUTE, minutes);
        return date = cal.getTime();
    }

    public static Date addHours(Date date, int hours) {
        if (date == null)
            return null;
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.add(Calendar.HOUR_OF_DAY, hours);
        return date = cal.getTime();
    }

    /**
     * author Bruce Zhang
     * 
     * @param dateStr
     *            时间字符串
     * @return 时间过滤显示函数,如10分钟前
     */
    public static String getDate(Object dateStr) {
        try {
            SimpleDateFormat format2 = new SimpleDateFormat("MM月dd日 HH:mm");
            SimpleDateFormat format3 = new SimpleDateFormat("yyyy-MM-dd HH:mm");
            Date date = new Date();
            if (dateStr == null) {
                return "";
            } else {
                if (dateStr instanceof String) {
                    if ("".equals(dateStr)) {
                        return "";
                    } else {
                        date = format3.parse(dateStr.toString());
                    }
                } else if (dateStr instanceof Date) {
                    date = (Date) dateStr;
                } else {
                    return "";
                }
                int nowYear = Calendar.getInstance().get(Calendar.YEAR);
                int year = Integer.parseInt(format3.format(date).substring(0, 4));
                long chaDate = new Date().getTime() - date.getTime();
                if (chaDate / (1000 * 60) < 1) {
                    return "1分钟前";
                } else if (chaDate / (1000 * 60) < 60) {
                    return chaDate / (1000 * 60) + "分钟前";
                } else if (chaDate / (1000 * 60 * 60) < 24) {
                    return chaDate / (1000 * 60 * 60) + "个小时前";
                } else if (year < nowYear) {
                    return format3.format(date);
                } else if (chaDate / (1000 * 60 * 60 * 24) < 3) {
                    return chaDate / (1000 * 60 * 60 * 24) + "天前";
                } else {
                    return format2.format(date);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            return "";
        }
    }

    /**
     * author Bruce Zhang
     * 
     * @param pattern
     *            日期格式：如"年"，"月"
     * @param target
     *            距离当前几天或几个小时
     * @return 具体时间
     */
    public static String getTargetTime(String pattern, Integer target) {
        long t = 0;
        if ("年".equals(pattern)) {
            t = year;
        } else if ("月".equals(pattern)) {
            t = month;
        } else if ("日".equals(pattern)) {
            t = day;
        } else if ("时".equals(pattern)) {
            t = hour;
        } else if ("分".equals(pattern)) {
            t = minute;
        } else if ("秒".equals(pattern)) {
            t = second;
        }
        t = new Date().getTime() - t * target;
        DateFormat f = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return f.format(new Date(t));
    }

    public static void deleteImage(String webAppPath, String imageFile) throws AppBusinessException {
        try {
            String fileName = webAppPath + File.separator + imageFile;
            File file = new File(fileName);
            if (file.exists()) {
                file.delete();
            }
        } catch (Exception e) {
            throw new AppBusinessException("删除图片失败", e);
        }

    }

    public static int strlen(String text) {
        return strlen(text, CHARSET);
    }

    public static int strlen(String text, String charsetName) {
        if (text == null || text.length() == 0) {
            return 0;
        }
        int length = 0;
        try {
            length = text.getBytes(charsetName).length;
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return length;
    }

    public static boolean isEmail(String email) {
        return Common.strlen(email) > 6 && email.matches("^[\\w\\-\\.]+@[\\w\\-\\.]+(\\.\\w+)+$");
    }

    public static String stripSlashes(String text) {
        if (text == null || text.equals("")) {
            return "";
        }
        StringBuffer sb = new StringBuffer(text.length());
        StringCharacterIterator iterator = new StringCharacterIterator(text);
        char character = iterator.current();
        while (character != StringCharacterIterator.DONE) {
            switch (character) {
                case '\'':
                    sb.append("'");
                    break;
                case '"':
                    sb.append('"');
                    break;
                case '\\':
                    sb.append(iterator.next());
                    break;
                default:
                    sb.append(character);
                    break;
            }
            character = iterator.next();
        }
        return sb.toString();
    }

    public static String addSlashes(String text) {
        if (text == null || text.equals("")) {
            return "";
        }
        StringBuffer sb = new StringBuffer(text.length() * 2);
        StringCharacterIterator iterator = new StringCharacterIterator(text);
        char character = iterator.current();
        while (character != StringCharacterIterator.DONE) {
            switch (character) {
                case '\'':
                case '"':
                case '\\':
                    sb.append("\\");
                default:
                    sb.append(character);
                    break;
            }
            character = iterator.next();
        }
        return sb.toString();
    }

    public static String fileext(String filePath) {
        int sl = filePath.length();
        if (sl < 2) {
            return "";
        }
        int lastPoint = filePath.lastIndexOf(".");
        if (lastPoint < 0) {
            return "";
        }
        return filePath.substring(lastPoint + 1, filePath.length());
    }

    public static ImageWeb getImageWebUrl(String imageServer, String photoUrl) throws IOException {
        ImageWeb imageWeb = new ImageWeb();
        URL url = new URL(imageServer + photoUrl);
        BufferedImage bi = ImageIO.read(url.openStream());
        if (bi != null) {
            imageWeb.setHeight(bi.getHeight());
            imageWeb.setWidth(bi.getWidth());
            imageWeb.setImageUrl(photoUrl);
            imageWeb.setImageServer(imageServer);
        }
        return imageWeb;
    }

    public static String getDomain(String url) {
        if (empty(url))
            return null;
        try {
            Pattern p = Pattern.compile("^(http|ftp)://.*.(com|cn|net|org|biz|info|cc|tv)", Pattern.CASE_INSENSITIVE);
            Matcher matcher = p.matcher(url);
            matcher.find();
            return matcher.group();
        } catch (Exception e) {
            return null;
        }

    }

    /**
     * author Bruce Zhang
     * 
     * @param pageCount
     *            >=0的整数
     * @return 0~pageCount的整数
     */
    public static Integer getRandom(int pageCount) {
        if (pageCount <= 0) {
            return 1;
        }
        Random random1 = new Random();
        return random1.nextInt(pageCount) + 1;
    }

    public static boolean randomSuccess(int effect) {
        Random random = new Random();
        int number = random.nextInt(100);
        if (number <= effect)
            return true;
        return false;
    }

    // 判断是传入内容否含有图片
    public static List<String> getContainedImg(String str) {
        List<String> list = new ArrayList<String>();
        Pattern p = Pattern.compile("<img[^>]+src\\s*=\\s*['\"]([^'\"]+)['\"][^>]*>");
        Matcher m = p.matcher(str);
        if (m.find()) {
            list.add(m.group(1));
        }
        return list;
    }

    public static String parseListToString(List<String> doorplateList) {
        StringBuffer doorplate = new StringBuffer("");
        if (doorplateList == null || doorplateList.size() == 0) {
            doorplate.append("('')");
        } else {
            for (int i = 0; i < doorplateList.size(); i++) {
                if (i == 0) {
                    doorplate.append("('" + doorplateList.get(i) + "'");
                } else {
                    doorplate.append(",'" + doorplateList.get(i) + "'");
                }
                if (i == doorplateList.size() - 1) {
                    doorplate.append(")");
                }
            }
        }
        return doorplate.toString();
    }

    public static byte[] picUrlToByte(String picUrl) {
        URL urlObj;
        byte[] b = null;
        try {
            urlObj = new URL(picUrl);
            URLConnection connection = urlObj.openConnection();
            InputStream is = connection.getInputStream();
            b = inputStreamToByte(is);
        } catch (MalformedURLException e) {
            // e.printStackTrace();
        } catch (IOException e) {
            // e.printStackTrace();
        }

        return b;
    }

    /**
     * inputstream 转字节数组
     * 
     * @param is
     * @return
     * @throws IOException
     */
    public static byte[] inputStreamToByte(InputStream is) throws IOException {
        ByteArrayOutputStream bytestream = new ByteArrayOutputStream();
        int ch;
        while ((ch = is.read()) != -1) {
            bytestream.write(ch);
        }
        byte imgdata[] = bytestream.toByteArray();
        bytestream.close();
        return imgdata;
    }

    public static String atContentRep2(String content) {
        if (Common.empty(content))
            return null;
        String result = content.replaceAll("@[\\u4e00-\\u9fa5\\w\\-]+",
            "<a class='u_name' style='color: #E2287A;' href=\"javascript:;\" target=\"_blank\">$0</a>");
        return result;
    }

    public static String atContentRep(String content) {
        String result = content.replaceAll("@[\\u4e00-\\u9fa5\\w\\-]+",
            "<a class='u_name' style='color: #E2287A;' href=\"javascript:;\" target=\"_blank\">$0</a>");
        return result;
    }

    // URL编码
    public static String URLEncode(String str) {
        if (emptyStr(str))
            return "";
        try {
            return java.net.URLEncoder.encode(str.trim(), "UTF-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
            return "";
        }
    }

    // URL解码
    public static String URLDecode(String str) {
        if (emptyStr(str))
            return "";
        try {
            return java.net.URLDecoder.decode(str.trim(), "UTF-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
            return "";
        }
    }

    // 获取文本url
    public static Boolean checkUrl(String content) {
        String reg = ".*[(http|ftp|file|www)]://.*";
        Pattern p = Pattern.compile(reg);
        Matcher m = p.matcher(content);
        if (m.matches()) {
            return true;
        } else {
            return false;
        }
    }

    // 获取真实IP
    public static String getIp(HttpServletRequest request) {
        String ip = request.getHeader("X-Real-IP");
        if (emptyStr(ip)) {
            ip = request.getRemoteAddr();
        }
        return ip;
    }

    public static String parseGuouojUrl(String pUrl, String memberName) {
        if (Common.empty(pUrl))
            return null;
        String regex = "(http:|https:)//[^[A-Za-z0-9\\._\\?%&+\\-=/#]]*";
        Pattern pattern = Pattern.compile(regex);
        Matcher matcher = pattern.matcher(pUrl);
        StringBuffer result = new StringBuffer();
        while (matcher.find()) {
            StringBuffer replace = new StringBuffer();
            String url = matcher.group();
            if ((url.startsWith("http://www.guoguojie.com") && !url.startsWith("http://www.guoguojie.com/images/expression/"))
                || ("果果妈".equals(memberName) && !url.startsWith("http://www.guoguojie.com/images/expression/"))) {
                replace.append("<a href=\"").append(url);
                replace.append("\" target=\"_blank\">" + url + "</a>");
            } else {
                replace.append(url);
            }

            matcher.appendReplacement(result, replace.toString());
        }
        matcher.appendTail(result);
        return result.toString();
    }

    // 获取参数值
    public static String getParaValue(String url, String parameter) throws Exception {
        URL aURL = new URL(url);

        String query = aURL.getQuery();
        if (query != null && !query.equals("")) {
            String[] paras = query.split("&");
            for (String para : paras) {
                String[] kv = para.split("=");
                if (kv[0] != null && kv[0].equals(parameter)) {
                    return kv[1];
                }
            }
        }
        return url;
    }

    public static void main(String[] args) throws Exception {
        // SimpleDateFormat simpleDateFormat = new
        // SimpleDateFormat("EEE MMM dd HH:mm:ss z yyyy");
        // Date s = simpleDateFormat.parse("Tue Mar 06 16:07:47 CST 2012");
        System.out.println(new SimpleDateFormat("yyyy:MM:dd HH:mm:ss").format(new Date()));
    }

}